﻿Public Class Busqueda_temas
    Dim conexion_bd As String

    Private Sub btn_nuevo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_nuevo.Click
        Dim temas As New Abm_temas()
        temas.ShowDialog()
    End Sub

    Private Sub btn_buscar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_buscar.Click
        If validar_campos_de_busqueda() Then
            Try
                Dim conexion As New AccesoADatos(conexion_bd)
                carga_lista(conexion.leerTabla("*", "Temas", "Descripcion like '%" & Me.txt_nombre.Text & "%'"))
            Catch ex As OleDb.OleDbException
                MessageBox.Show("Debe completar los campos correctamente", "Error de búsqueda", MessageBoxButtons.OK, MessageBoxIcon.Warning)
                txt_nombre.Clear()
                carga_lista(Me.leer_tabla())
            End Try
        Else
            MessageBox.Show("Debe completar todos los campos", "Error en la búsqueda", MessageBoxButtons.OK, MessageBoxIcon.Warning)
        End If
    End Sub

    Private Function validar_campos_de_busqueda() As Boolean
        Dim validacion As Boolean = True
        If txt_nombre.Text = "" Then
            validacion = False
        End If
        Return validacion
    End Function

    Private Sub Busqueda_temas_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.conexion_bd = "Provider=SQLOLEDB;Data Source=GTºN1\SQLEXPRESS;Integrated Security=SSPI;Initial Catalog=C:\DB\LIBRERIA_MAGOO.MDF"
        Me.carga_lista(Me.leer_tabla())
    End Sub

    Private Sub carga_lista(ByVal tabla As Data.DataTable)
        Me.lst_temas.DataSource = tabla
        Me.lst_temas.ValueMember = "IdTema"
        Me.lst_temas.DisplayMember = "Descripcion"
    End Sub

    Private Function leer_tabla() As Data.DataTable
        Dim conexion As New Data.OleDb.OleDbConnection
        Dim command As New Data.OleDb.OleDbCommand
        Dim tabla As New Data.DataTable 'declaro la variable tabla'

        'abro conexion
        conexion.ConnectionString = Me.conexion_bd
        conexion.Open()

        command.Connection = conexion
        command.CommandType = CommandType.Text
        command.CommandText = "select * from Temas"

        tabla.Load(command.ExecuteReader())
        conexion.Close()
        Return tabla
    End Function

    Private Sub txt_nombre_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txt_nombre.TextChanged
        If (txt_nombre.Text = "") Then
            carga_lista(Me.leer_tabla())
        End If
    End Sub

    Private Sub btn_agregar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_agregar.Click
        If (Not (lst_temas.SelectedItem Is Nothing)) Then
            If (Not (lst_temasAgregados.Items.Contains(lst_temas.Text))) Then
                lst_temasAgregados.Items.Add(lst_temas.Text)
            Else
                MessageBox.Show("El tema elegido ya está en la lista.", "Aviso", MessageBoxButtons.OK, MessageBoxIcon.Information)
            End If
        End If
    End Sub

    Private Sub btn_quitar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_quitar.Click
        If (Not (lst_temasAgregados.SelectedItem Is Nothing)) Then
            lst_temasAgregados.Items.Remove(lst_temasAgregados.SelectedItem)
        End If
    End Sub

    Private Sub btn_aceptar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_aceptar.Click
        Dim frm_libros As Abm_libros
        frm_libros = Owner
        Dim ban As Integer = 0
        Dim listaTemas As New List(Of String)()
        For i As Integer = 0 To (lst_temasAgregados.Items.Count - 1)
            For j As Integer = 0 To (frm_libros.lst_temas.Items.Count - 1)
                If frm_libros.lst_temas.Items(j) <> lst_temasAgregados.Items(i) Then
                    ban = 0
                Else
                    ban = 1
                    Exit For
                End If
            Next
            If ban = 0 Then
                frm_libros.lst_temas.Items.Add(lst_temasAgregados.Items(i))
            End If
        Next
        Me.Close()
    End Sub

    Private Sub btn_cancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_cancelar.Click
        Dim mensaje As String = "¿Esta seguro que desea cancelar la operación actual?"
        If MessageBox.Show(mensaje, "Confirmación", MessageBoxButtons.YesNo, _
                           MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
            Me.Close()
        End If
    End Sub
End Class